package random

import (
	"math/rand"
	"strconv"
)

// Color 随机生成一个有吸引力的颜色，格式为 '#RRGGBB'。
func Color(name string) string {
	color, ok := COLOR_DICT[name]
	if ok {
		return color["nicer"]
	}
	return Hex()
}

func Hex() string {
	var hsv = _goldenRatioColor()
	var rgb = hsv2rgb(hsv)
	var hex = rgb2hex(rgb[0], rgb[1], rgb[2])
	return hex
}

func Rgb() string {
	var hsv = _goldenRatioColor()
	var rgb = hsv2rgb(hsv)
	return "rgb(" +
		strconv.Itoa(rgb[0]) + ", " +
		strconv.Itoa(rgb[1]) + ", " +
		strconv.Itoa(rgb[2]) + ")"
}

func Rgba() string {
	var hsv = _goldenRatioColor()
	var rgb = hsv2rgb(hsv)
	return "rgba(" +
		strconv.Itoa(rgb[0]) + ", " +
		strconv.Itoa(rgb[1]) + ", " +
		strconv.Itoa(rgb[2]) + ", " +
		strconv.FormatFloat(rand.Float64(), 'g', 2, 64) + ")"
}

func Hsl() string {
	var hsv = _goldenRatioColor()
	var hsl = hsv2hsl(hsv)
	return "hsl(" +
		strconv.Itoa(int(hsl[0])) + ", " +
		strconv.Itoa(int(hsl[1])) + ", " +
		strconv.Itoa(int(hsl[2])) + ")"
}

func _goldenRatioColor(values ...float64) []float64 {
	var (
		_goldenRatio = 0.618033988749895
		_hue         = rand.Float64()

		saturation float64
		value      float64
	)

	_hue += _goldenRatio
	_hue = _hue - float64(int(_hue))

	if len(values) >= 1 {
		saturation = values[0]
	} else {
		saturation = 0.5
	}
	if len(values) >= 2 {
		value = values[1]
	} else {
		value = 0.95
	}

	return []float64{
		_hue * 360,
		saturation * 100,
		value * 100,
	}
}
